package com.jim.study.delicacy_bjx.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jim.study.delicacy_bjx.dao.UserMapper;
import com.jim.study.delicacy_bjx.entity.Preference;
import com.jim.study.delicacy_bjx.entity.User;
import com.jim.study.delicacy_bjx.until.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 添加用户
     * @param username
     * @param password
     * @return
     */
    public User insertUser(String username,String password){
        String id = idWorker.nextId()+"";
        User user = new User(id,username,password,"","");
        userMapper.insertUser(user);
        return user;
    }

    /**
     * 添加用户偏好
     * @param data
     */
    public void insertPreference(JSONObject data){
        String username = data.get("username").toString();
        JSONArray preferences = data.getJSONArray("preferences");
        //查询数据库中当前用户的偏好
        List<Preference> originPreferenceList = userMapper.getPreferenceByUsername(username);
        List<Preference> newPreferenceList = new ArrayList<>();
        preferences.forEach(p->{
            JSONObject jsonP = (JSONObject) JSON.toJSON(p);
            String preferenceId = jsonP.get("preference").toString();
            String type = jsonP.get("type").toString();
            int index = indexOf(originPreferenceList,preferenceId,type);
            if(index<0){
                String id = idWorker.nextId()+"";
                Preference preference = new Preference(id,username,preferenceId,type);
                newPreferenceList.add(preference);
            }
            else {
                originPreferenceList.remove(index);
            }
        });
        if(originPreferenceList.size()>0){
            //删除旧的爱好
            userMapper.removePreferences(originPreferenceList);
        }
        if(newPreferenceList.size()>0){
            //添加新的偏好
            userMapper.insertPreference(newPreferenceList);
        }
    }

    /**
     * 删除用户
     * @param id
     */
    public int deleteUserById(String id){
        return userMapper.deleteUserById(id);
    }

    /**
     * 更新用户信息
     * @param user
     */
    public void updateUser(User user){
        if(deleteUserById(user.getId())>0){
            userMapper.updateUser(user);
        }
    }

    private int indexOf(List<Preference> preferences,String preferenceId,String type){
        int index = -1;
        for (int i = 0; i < preferences.size(); i++) {
            Preference p = preferences.get(i);
            if(preferenceId.equals(p.getPreferenceId())&&type.equals(p.getType())){
                index = i;
                break;
            }
        }
        return index;
    }
}
